package com.example.myapplication.part20;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.List;
import java.util.Locale;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

    private List<Order> orders;

    public OrderAdapter(List<Order> orders) {
        this.orders = orders;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (orders == null || position >= orders.size()) {
            return; // 防止越界
        }
        Order order = orders.get(position);
        holder.tvOrderInfo.setText(String.format(
                Locale.getDefault(),
                "订单号：%s\n旅游项目：%s\n客户姓名：%s\n日期：%s %s\n人数：%d 总金额：%.2f",
                order.getOrderId(),
                order.getTourName(),
                order.getCustomerName(),
                order.getOrderDate(),
                order.getOrderTime(),
                order.getPeopleCount(),
                order.getTotalAmount()
        ));
    }

    @Override
    public int getItemCount() {
        return orders != null ? orders.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvOrderInfo;

        ViewHolder(View itemView) {
            super(itemView);
            tvOrderInfo = itemView.findViewById(R.id.tvOrderInfo);
        }
    }
}